<template>
  <div class="catrgory-select-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="一级分类">
        <el-select
          v-model="categoryIdForm.category1Id"
          :disabled="disabled"
          placeholder="请选择"
          @change="handleCategory1Change"
        >
          <el-option
            v-for="c1 in category1List"
            :key="c1.id"
            :label="c1.name"
            :value="c1.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          v-model="categoryIdForm.category2Id"
          :disabled="disabled"
          placeholder="请选择"
          @change="handleCategory2Change"
        >
          <el-option
            v-for="c2 in category2List"
            :key="c2.id"
            :label="c2.name"
            :value="c2.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          v-model="categoryIdForm.category3Id"
          :disabled="disabled"
          placeholder="请选择"
          @change="handleCategory3Change"
        >
          <el-option
            v-for="c3 in category3List"
            :key="c3.id"
            :label="c3.name"
            :value="c3.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'CategorySelect',
  components: {},
  props: {
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      categoryIdForm: {
        category1Id: '',
        category2Id: '',
        category3Id: ''
      },
      // 一级分类数据
      category1List: [],
      // 二级分类数据
      category2List: [],
      // 三级分类数据
      category3List: []
    }
  },
  computed: {},
  watch: {},
  created() {
    this.onGetCategory1List()
  },
  mounted() {},
  methods: {
    async onGetCategory1List() {
      const res = await this.$api.attr.getCategory1List()
      if (res.code === 200) {
        this.category1List = res?.data || []
      }
    },
    async handleCategory1Change() {
      const { category1Id } = this.categoryIdForm
      this.clearId('first')
      const res = await this.$api.attr.getCategory2List(category1Id)
      if (res.code === 200) {
        this.category2List = res?.data || []
      }
    },
    async handleCategory2Change() {
      const { category2Id } = this.categoryIdForm
      this.clearId('second')
      const res = await this.$api.attr.getCategory3List(category2Id)
      if (res.code === 200) {
        this.category3List = res?.data || []
      }
    },
    handleCategory3Change() {
      this.$emit('get-attr', this.categoryIdForm)
    },
    // 清空id
    clearId(type) {
      if (type === 'first') {
        this.categoryIdForm.category2Id = ''
        this.categoryIdForm.category3Id = ''
      } else if (type === 'second') {
        this.categoryIdForm.category3Id = ''
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
